# -*- coding: utf-8 -*-
'''
    :file: Spiders.py
    :author: -Farmer
    :url: https://blog.farmer233.top
    :date: 2021/02/17 03:17:17
'''
import time
import os
from threading import Thread
from urllib import request
import re
from flask.globals import current_app
from werkzeug.utils import secure_filename
from app.utils import is_safe_path, get_now_timestamp

# 离线下载功能
class DownloadSpider():
    def __init__(self, username) -> None:
        # 初始化下载根目录
        self.prefix = os.path.join(current_app.config['UPLOAD_FOLDER'], username)

    def download(self, get_url:str, save_path):
        # 用正则表达式，限制下载链接仅为http协议
        re_str = '(http|https):\/\/[^\s]*'
        if not re.match(re_str, get_url):
            raise ValueError('下载地址不合法')
        # 生成下载时间
        timeArray = time.localtime(time.time())
        now_time = time.strftime("%Y-%m-%d_%H:%M:%S", timeArray)
        # 序列化离线下载文件名
        # 文件名命名格式: 下载开始时间-下载文件名
        filename = now_time + "-" + get_url.split("/")[-1]
        filename = secure_filename(filename)

        if not is_safe_path(save_path):
            raise ValueError("保存路径不合法")

        if filename is None:
            raise ValueError("url解析失败")
        
        save_path = os.path.join(self.prefix, save_path)
        if not os.path.exists(save_path):
            raise ValueError('路径不存在')

        filename = os.path.join(save_path, filename)
        # 开启下载线程
        thr = Thread(target=_download, args=[get_url, filename])
        thr.start()
        return thr

# 下载实例
def _download(url, filename):
    request.urlretrieve(url=url, filename=filename)